package com.winning.util.excelutil.common.service.impl;

import com.alibaba.excel.metadata.BaseRowModel;
import com.winning.util.excelutil.common.dao.Column;
import com.winning.util.excelutil.common.dao.Table;
import com.winning.util.excelutil.common.exception.BaseException;
import com.winning.util.excelutil.common.service.ExcelImportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
/*import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;*/
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @作者：caopengfei
 * @时间：2020/2/13
 */
@Slf4j
@Service
public  class ExcelImportServiceImpl implements ExcelImportService {

/*    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;*/

    public List<BaseRowModel> doImport(List<Object> datas){
        String sqlStr = createInsertSql(datas.get(0));
        Map<String,Object> [] params = list2ParamMap(datas);
//        int [] results = namedParameterJdbcTemplate.batchUpdate(sqlStr,params);
        return null;
    }

    /**
    * @vsts_no：
    * @comment： 将数据集合转成Map
    * @author： caopengfei
    * @time：  2020/2/13 12:41
    */
    private Map<String,Object> [] list2ParamMap(List<Object> datas){
        Map<String,Object> [] maps = new Map[datas.size()];
        for (int i = 0; i < datas.size(); i++) {
            maps[i] = object2Map(datas.get(i));
        }
        return maps;
    }

    private Map<String,Object> object2Map(Object obj) {
        Class c = obj.getClass();
        //2.遍历所有的字段
        Field[] fArray = c.getDeclaredFields();
        Map<String, Object> param = new HashMap<>();
        for (Field field : fArray) {
            boolean fExists = field.isAnnotationPresent(Column.class);
            if (!fExists) {
                continue;
            }
            Column column = field.getAnnotation(Column.class);
            String columName = column.name();
            //4.2拿到字段值
            String fieldName = field.getName();
            String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
            Object fieldValue = null;
            try {
                Method getMethod = c.getMethod(getMethodName);
                fieldValue = getMethod.invoke(obj);
                param.put(columName, fieldValue);
            } catch (Exception e) {
                throw new BaseException("解析对象失败,获取参数值",e);
            }
        }
        return param;
    }

    /**
    * @vsts_no：
    * @comment： 拼装查询逻辑
    * @author： caopengfei
    * @time：   12:06
    */
    private  String createInsertSql(Object obj){
        StringBuilder insert = new StringBuilder();
        StringBuilder values = new StringBuilder();
        //1.获取到class
        Class c = obj.getClass();
        //2.获取到table的名字
        boolean exists = c.isAnnotationPresent(Table.class);
        if (!exists) {
            return null;
        }
        Table t = (Table) c.getAnnotation(Table.class);

        String tableName = t.value();

        insert.append(" insert into  ").append(tableName).append(" ( ");
        values.append(" values( ");
        //2.遍历所有的字段
        Field[] fArray = c.getDeclaredFields();
        for (Field field : fArray) {
            boolean fExists = field.isAnnotationPresent(Column.class);
            if (!fExists) {
                continue;
            }
            Column column = field.getAnnotation(Column.class);
            insert.append(column.name()).append(",");
            values.append(":").append(column.name()).append(",");
        }
        insert.deleteCharAt(insert.length()-1);
        insert.append(") ");
        values.deleteCharAt(values.length()-1);
        values.append(") ");
        insert.append(values);
        return insert.toString();
    }

}
